﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class news_ModuleEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            bd(true);
        }
    }

    void bd(bool flag)
    {
        string id=Request["id"];
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        var c = dal.PublishedModule.Where(x => x.ID == id).First();
        lblPageName.Text = c.PublishedPage.Name;
        lblModuleName.Text = c.Name;
        object [] o=new  object[c.Hangshu.Value];
        for (int i = 0; i < c.Hangshu; i++)
        {
            o[i]=new {ModuleId=id,Hangshu=i};
        }
        DataList1.DataSource = o;
        DataList1.DataBind();
        var module = dal.PublishedModule.Where(x => x.ID == id).First();
        string content = "";
        if (flag)
        {
            content = module.Content;
        }
        else
        {
            content = module.TempContent;
        }
         
            List<List<NUrl>> lst = new List<List<NUrl>>();


            if (string.IsNullOrEmpty(content))
            {
                lst = new List<List<NUrl>>();
            }
            else
            {
                lst = Newtonsoft.Json.JsonConvert.DeserializeObject<List<List<NUrl>>>(content);
            }
            for (int i = 0; i < DataList1.Items.Count; i++)
            {
                news_ModuleCell d = (news_ModuleCell)DataList1.Items[i].FindControl("ModuleCell1");
                if (i >= lst.Count)
                {
                    d.LoadData(null);
                }
                else
                {
                    d.LoadData(lst[i]);
                }
            }
      
    }

    protected void btnSaveTemp_Click(object sender, EventArgs e)
    {
        List<List<NUrl>> Lst = new List<List<NUrl>>();
        for (int i = 0; i < DataList1.Items.Count; i++)
        {
            news_ModuleCell c = (news_ModuleCell)DataList1.Items[i].FindControl("ModuleCell1");
           
            List<NUrl> nlst = new List<NUrl>();
            for (int j = 1; j <= 9; j++)
            {
                string txtTitle = "txtTitle" + j.ToString();
                NUrl url = new NUrl();
                url.Text = ((TextBox)c.FindControl(txtTitle)).Text;
                if (string.IsNullOrEmpty(url.Text))
                {
                    continue;
                }
                string txtUrl = "txtValue" + j.ToString();
                url.Url = ((TextBox)c.FindControl(txtUrl)).Text;
                string hidColor = "hidColor" + j.ToString();
                url.Color = ((HiddenField)c.FindControl(hidColor)).Value;
                string hidBold="hidBold"+j.ToString();
                var hid = (HiddenField)c.FindControl(hidBold);
                url.FontWeight = hid.Value;
                nlst.Add(url);
            }
            if(nlst.Count>0)
                Lst.Add(nlst);
        }
        string dd= Newtonsoft.Json.JsonConvert.SerializeObject(Lst);
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
         string id=Request["id"];
        var module=  dal.PublishedModule.Where(x => x.ID == id).First();
        module.TempContent = dd;
        dal.SaveChanges();
        bd(false);
    }
    protected void btnImport_Click(object sender, EventArgs e)
    {
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        string id = Request["id"];
        var module = dal.PublishedModule.Where(x => x.ID == id).First();
        List<List<NUrl>> lst = new List<List<NUrl>>();
        if (string.IsNullOrEmpty(module.Content))
        {
            lst = new List<List<NUrl>>();
        }
        else
        {
            lst = Newtonsoft.Json.JsonConvert.DeserializeObject<List<List<NUrl>>>(module.Content);
        }
        for (int i = 0; i < DataList1.Items.Count; i++)
        {
            news_ModuleCell c = (news_ModuleCell)DataList1.Items[i].FindControl("ModuleCell1");
            if (i >= lst.Count)
            {
                c.LoadData(null);
            }
            else
            {
                c.LoadData(lst[i]);
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        List<List<NUrl>> Lst = new List<List<NUrl>>();
        for (int i = 0; i < DataList1.Items.Count; i++)
        {
            news_ModuleCell c = (news_ModuleCell)DataList1.Items[i].FindControl("ModuleCell1");

            List<NUrl> nlst = new List<NUrl>();
            for (int j = 1; j <= 9; j++)
            {
                string txtTitle = "txtTitle" + j.ToString();
                NUrl url = new NUrl();
                url.Text = ((TextBox)c.FindControl(txtTitle)).Text;
                if (string.IsNullOrEmpty(url.Text))
                {
                    continue;
                }
                string txtUrl = "txtValue" + j.ToString();
                url.Url = ((TextBox)c.FindControl(txtUrl)).Text;
                string hidColor = "hidColor" + j.ToString();
                url.Color = ((HiddenField)c.FindControl(hidColor)).Value;
                string hidBold = "hidBold" + j.ToString();
                var hid = (HiddenField)c.FindControl(hidBold);
                url.FontWeight = hid.Value;
                nlst.Add(url);
            }
            if (nlst.Count > 0)
                Lst.Add(nlst);
        }
        string dd = Newtonsoft.Json.JsonConvert.SerializeObject(Lst);
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        string id = Request["id"];
        var module = dal.PublishedModule.Where(x => x.ID == id).First();
        module.Content = dd;
        dal.SaveChanges();
        string [] keys= dal.MemcacheKey.Where(x => x.Key.Contains(id)).Select(x => x.Key).ToArray();
        foreach (var d in keys)
        {
            MemcachedTools.Clear(d);
        }
        bd(true);
    }
    protected void btnImportTemp_Click(object sender, EventArgs e)
    {
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        string id = Request["id"];
        var module = dal.PublishedModule.Where(x => x.ID == id).First();
        List<List<NUrl>> lst = new List<List<NUrl>>();
        if (string.IsNullOrEmpty(module.TempContent))
        {
            lst = new List<List<NUrl>>();
        }
        else
        {
            lst = Newtonsoft.Json.JsonConvert.DeserializeObject<List<List<NUrl>>>(module.TempContent);
        }
        for (int i = 0; i<DataList1.Items.Count; i++)
        {
            news_ModuleCell c = (news_ModuleCell)DataList1.Items[i].FindControl("ModuleCell1");
            if (i >= lst.Count)
            {
                c.LoadData(null);
            }
            else
            {
                c.LoadData(lst[i]);
            }
        }
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < DataList1.Items.Count; i++)
        {
            news_ModuleCell c = (news_ModuleCell)DataList1.Items[i].FindControl("ModuleCell1");
            c.LoadData(null);
        }
    }
}